﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Checkers2
{
    public partial class Form1 : Form
    {
        #region vars
        PictureBox currentCell = new PictureBox();
        Pawns currentPawn = new Pawns();
        PictureBox availibleCellL;
        PictureBox availibleCellR;
        PictureBox availibleCellLB;
        PictureBox availibleCellRB;
        PictureBox[] cells = new PictureBox[64];
        Pawns[] whiteSet = new Pawns[12];
        Pawns[] blackSet = new Pawns[12];
        Pawns[] allPawns = new Pawns[24];
        Point toLeft;
        Point toRight;
        Point toLeftB;
        Point toRightB;
        bool currentTurnIsW = true;
        public Form1()
        #endregion

        #region PawnArrays
        {
            InitializeComponent();
            int c = 0;
            int c2 = 1;
            int c4 = 13;
            foreach (PictureBox p in this.Controls.OfType<PictureBox>())
            {
                p.Click += new System.EventHandler(this.Ucell_Click);
                if (c == 0 || c % 2 == 0)
                {
                    p.BackColor = System.Drawing.Color.Black;
                }
                cells[c] = p;
                c++;
            }
            foreach (Label l in this.Controls.OfType<Label>())
            {
                l.Click += new System.EventHandler(this.ULabel_Click);
            }
            
            for (int c3 = 0; c3 < 12;c3++)
            {
                whiteSet[c3] = new Pawns();
                whiteSet[c3].IsWhite = true;
                foreach(Control cont in this.Controls)
                {
                    if (cont.Name == "label" + c2.ToString())
                    {
                        whiteSet[c3].Model = (Label)cont;
                    }
                }
                c2++;
                whiteSet[c3].X = whiteSet[c3].Model.Location.X;
                whiteSet[c3].Y = whiteSet[c3].Model.Location.Y;
            }

            for (int c3 = 0; c3 < 12; c3++)
            {
                blackSet[c3] = new Pawns();
                blackSet[c3].IsWhite = false;
                foreach (Control cont in this.Controls)
                {
                    if (cont.Name == "label" + c4.ToString())
                    {
                        blackSet[c3].Model = (Label)cont;
                    }
                }
                c4++;
                blackSet[c3].X = blackSet[c3].Model.Location.X;
                blackSet[c3].Y = blackSet[c3].Model.Location.Y;
            }
            for (int c3 = 0; c3 < 24; c3++)
            {
                if(c3<12)
                {
                    allPawns[c3] = whiteSet[c3];
                }
                else
                {
                    allPawns[c3] = blackSet[c3 - 12];
                }
            }
        }
        #endregion

        public object StrToObj(string name)
        {
            var arr = this.Controls.Find(name, true);
            return arr;
        }

        protected void ULabel_Click(object sender, EventArgs e)
        {
            Label clickedLabel = (Label)sender;
            foreach (Pawns pawn in allPawns)
            {
                if (pawn.Model == clickedLabel) 
                {
                    currentPawn = pawn;
                }
            }
            if (currentTurnIsW == currentPawn.IsWhite)
            {
                currentPawn.IsSelected = true;
                Point currentPawnLoc = new Point();
                currentPawnLoc = currentPawn.Model.Location;
                FindAdjustCell(currentPawnLoc);
            }
            
        }

        private void Ucell_Click(object sender, EventArgs e)
        {
            PictureBox clickedCell = (PictureBox)sender;
#region Move
            if (!CellHasPawn(clickedCell))
            {
                if (sender == availibleCellL)
                {
                    currentPawn.Move(toLeft);
                }
                if (sender == availibleCellR)
                {
                    currentPawn.Move(toRight);
                }
            }
#endregion


            else
            {
                #region EatLeft
                if (sender == availibleCellL)
                {
                    Point loc = toLeft;
                    PictureBox cellWithEatenPwn = availibleCellL;
                    Pawns eatenPawn = new Pawns();
                    foreach (Pawns p in allPawns)
                    {
                        if (cellWithEatenPwn.Bounds.Contains(new Point(p.X, p.Y)))
                        {
                            eatenPawn = p;
                        }
                    }
                    FindAdjustCell(loc);
                    if (!CellHasPawn(availibleCellL))
                    {
                        eatenPawn.DeletePawn();
                        currentPawn.Move(toLeft);
                    }
                }
                #endregion
                #region EatRight
                if (sender == availibleCellR)
                {
                    Point loc = toRight;
                    PictureBox cellWithEatenPwn = availibleCellR;
                    Pawns eatenPawn = new Pawns();
                    foreach (Pawns p in allPawns)
                    {
                        if (cellWithEatenPwn.Bounds.Contains(new Point(p.X, p.Y)))
                        {
                            eatenPawn = p;
                        }
                    }
                    FindAdjustCell(loc);
                    if (!CellHasPawn(availibleCellR))
                    {
                        eatenPawn.DeletePawn();
                        currentPawn.Move(toRight);
                    }
                }
                #endregion
                #region EatLeftB
                if (sender == availibleCellLB)
                {
                    Point loc = toLeftB;
                    PictureBox cellWithEatenPwn = availibleCellLB;
                    Pawns eatenPawn = new Pawns();
                    foreach (Pawns p in allPawns)
                    {
                        if (cellWithEatenPwn.Bounds.Contains(new Point(p.X, p.Y)))
                        {
                            eatenPawn = p;
                        }
                    }
                    FindAdjustCell(loc);
                    if (!CellHasPawn(availibleCellLB))
                    {
                        eatenPawn.DeletePawn();
                        currentPawn.Move(toLeftB);
                    }
                }
                #endregion
                #region EatRightB
                if (sender == availibleCellRB)
                {
                    Point loc = toRightB;
                    PictureBox cellWithEatenPwn = availibleCellRB;
                    Pawns eatenPawn = new Pawns();
                    foreach (Pawns p in allPawns)
                    {
                        if (cellWithEatenPwn.Bounds.Contains(new Point(p.X, p.Y)))
                        {
                            eatenPawn = p;
                        }
                    }
                    FindAdjustCell(loc);
                    if (!CellHasPawn(availibleCellRB))
                    {
                        eatenPawn.DeletePawn();
                        currentPawn.Move(toRightB);
                    }
                }
            }
                #endregion
            currentTurnIsW = !currentTurnIsW;
        }

        private bool CellHasPawn(PictureBox cell)
        {
            bool result = false;
            foreach (Control c in this.Controls)
            {
                
                if(cell.Bounds.Contains(c.Location)&&c!=cell)
                {
                    result = true;
                    break;
                }
            }
            return result;
        }

        private void FindAdjustCell(Point pwnLocation)
        {
            foreach (PictureBox p in cells)
            {
                if (p.Bounds.Contains(pwnLocation))
                {
                    currentCell = p;
                }

                toLeft.X = pwnLocation.X - 80;
                toRight.X = pwnLocation.X + 80;
                toLeftB.X = pwnLocation.X - 80;
                toRightB.X = pwnLocation.X + 80;
                if (currentPawn.IsWhite)
                {
                    toLeft.Y = pwnLocation.Y + 66;
                    toRight.Y = pwnLocation.Y + 66;
                    toLeftB.Y = pwnLocation.Y - 66;
                    toRightB.Y = pwnLocation.Y - 66;
                }
                if (!currentPawn.IsWhite)
                {
                    toLeft.Y = pwnLocation.Y - 66;
                    toRight.Y = pwnLocation.Y - 66;
                    toLeftB.Y = pwnLocation.Y + 66;
                    toRightB.Y = pwnLocation.Y + 66;
                }
                if (p.Bounds.Contains(toLeft))
                {
                    availibleCellL = p;
                }
                if (p.Bounds.Contains(toRight))
                {
                    availibleCellR = p;
                }
                if (p.Bounds.Contains(toLeftB))
                {
                    availibleCellLB = p;
                }
                if (p.Bounds.Contains(toRightB))
                {
                    availibleCellRB = p;
                }
            }
        }
       
    }
}
